<?php

namespace app\admin\controller;

use think\Loader;
use think\Db;

class Symbol extends Common
{
    /*
     * 产品列表
     * */
    public function index()
    {
        $sou = request()->param();
//        echo input("recommend").$sou['recommend'];
        $cate_id = input("cate_id")?"s.cate_id = ".intval(input("cate_id")):($sou["cate_id"]="1=1");
        $name = input("name")?input("name"):($sou["name"]="");
        $on_sale = input("on_sale")?"s.on_sale = '".input("on_sale")."'":($sou["on_sale"]="s.id=s.id");
        $recommend = "s.name=s.name";
        $sou["recommend"]="";
        if(input("recommend")=="是"){
            $recommend = "s.recommend = '1'";
            $sou["recommend"]="是";
        }elseif (input("recommend")=="否"){
            $recommend = "s.recommend = '0'";
            $sou["recommend"]="否";
        }


        $DB = Db::name("symbol")
            ->alias("s")
            ->join("symbol_cat sc", "sc.id=s.cate_id")
            ->where("s.status != 0")
            ->where("s.status != -1")
            ->where($cate_id)
            ->where("s.name like '%$name%'")
            ->where($on_sale)
            ->where($recommend)
            ->order("s.sort asc")
            ->field(["s.id id", "sc.title title", "s.cate_id", "s.name", "s.on_sale", "s.overfee", "s.servicefee", "s.deposit", "s.maxrise", "s.maxlost", "s.status", "s.recommend", "s.created_at", "s.leverage", "s.updated_at", "s.date"])
            ->paginate(10, false, ['query' => request()->param()]);
//        print_r($DB);
//        echo DB::table('symbol')->getlastsql();del();
//        $rs = $DB->render();
//        echo $sou["recommend"];del();
        $symbol_cat = Db::name("symbol_cat")->field(["id", "title"])->select();
        return view("index", ["arr" => $DB, "sou" => $sou, "symbol_cat" => $symbol_cat]);
    }

    /*
     * 产品添加
     * get:查询出杠杆，商户
     * */
    public function add()
    {
        if (isset($_POST["sub"])) {
//            validate验证器未做
            $data = input("post.");
            $data['lever_id'] = implode(",", $data['lever_id']);
            $data['created_at'] = time();
            unset($data['sub']);
            $insert = Db::name("symbol")->insert($data);

            print_r($data);
            if ($insert) {
                $str = "操作成功";
            } else {
                $str = "操作失败";
            }
            return $this->success($str, "/admin/symbol/index");
        } else {
            $lever = Db::name("lever")->field(["id", "name"])->select();
            return view("add", ["lever" => $lever]);
        }
    }

    /*
     *产品修改
     * */
    public function update()
    {
        if(isset($_POST['update'])){
            $data = input("post.");
            $data['lever_id'] = implode(",", $data['lever_id']);
            $data['updated_at'] = time();
            unset($data['update']);
            $id = intval(input("id"));
            unset($data['id']);
            $update = Db::name("symbol")->where("id",$id)->update($data);
            print_r($data);
            if ($update) {
                $str = "操作成功";
            } else {
                $str = "操作失败";
            }
//            echo $str;
            return $this->success($str, "/admin/symbol/index");
        }else{
            $DB = Db::name("symbol")
                ->alias("s")
                ->join("symbol_cat sc", "sc.id=s.cate_id")
                ->where("s.status != 0")
                ->where("s.id",intval(input("id")))
                ->order("s.sort asc")
                ->field(["s.id id", "sc.title title", "s.cate_id", "s.name", "s.on_sale", "s.overfee", "s.servicefee", "s.deposit", "s.maxrise", "s.maxlost",
                    "s.status", "s.recommend", "s.created_at", "s.leverage", "s.updated_at", "s.date","s.code","s.code2","s.name_code","s.rise_fall","desc",
                    "contract_size","zoom_size","trade_day","unit","unit_price","unit_step","minrise","minlost","s.sort","risk","risk_auto","digit","s.overtime","trade_time","lever_id"])
                ->find();
            $lever = Db::name("lever")->field(["id", "name"])->select();
            $DB["lever_id"]=explode(",",$DB["lever_id"]);
//            print_r($DB);del();
            return view("update",["arr"=>$DB,"lever" => $lever]);
        }
    }

    /*
     * 产品删除
     * */
    public function del()
    {
        $id = intval(input("id"));
        $DB = Db::name("symbol")->where("id",$id)->update(["status"=>"-1"]);
        if($DB){
            $status="success";
            $msg="删除成功";
        }else{
            $status="fail";
            $msg="删除失败";
        }
        return json(['status'=>$status,'msg'=>$msg]);
    }

    /*
     * 获取产品分类
     * */
    public function cate()
    {
        $id = intval(input("id"));
        $symbol_cat = Db::name("symbol_cat")->where("merchant_id", $id)->field(["id", "title"])->select();
        $str = "<option value=''>请选择产品类型</option>";
        foreach ($symbol_cat as $sc) {
            $str .= "<option value='" . $sc["id"] . "'>" . $sc["title"] . "</option>";
        }
        echo $str;
    }
}
















































